package b4.model.town
{
	import b4.model.basic.*;
	import b4.net.*;
	
	public class TownModel extends Model
	{
		//唯一号
		public var id:int;
		//城镇名字
		public var name:String;
		//建筑列表 {id => buildingModel}
		public var buildingList:Object;
		//是否主城
		public var mainFlag:Boolean;
		
		public function TownModel()
		{
			buildingList = new Object();
		}
		
		//获得城镇信息
		public function getTownInfo():void
		{
			var serverRequest:ServerRequest = new ServerRequest(ServerEvent.GET_TOWN_INFO, {id:id});
			serverRequest.addEventListener(ServerEvent.GET_TOWN_INFO, onGetTownInfoComplete);
			serverRequest.post(true);
		}
		
		private function onGetTownInfoComplete(event:ServerEvent):void
		{
			var jsonObj:Object = event.jsonObj;
			this.loadbyObject(jsonObj.data.townInfo);
			//获得建筑列表
			buildingList = new Object();
			for (var idStr:String in jsonObj.data.buildingList)
			{
				var buildingModel:BuildingModel = new BuildingModel();
				buildingModel.loadbyObject(jsonObj.data.buildingList[idStr]);
				buildingList[buildingModel.id] = buildingModel;
			}
			var modelEvent:ModelEvent = new ModelEvent(ModelEvent.TOWN_REFRESH);
			modelEvent.jsonObj = jsonObj;
			dispatchEvent(modelEvent);
		}
		
	}
}